package com.itheima.service.impl;

import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.LoginInfo;
import com.itheima.service.LoginService;
import com.itheima.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private EmpMapper empMapper;
    @Override
    public LoginInfo login(Emp emp) {
        Emp empInfo = empMapper.findByuserNameAndPassword(emp);
        if(empInfo != null){
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("id", empInfo.getId());
            dataMap.put("username", empInfo.getUsername());
            String jwt = JwtUtils.generateJwt(dataMap);
            return new LoginInfo(empInfo.getId(), empInfo.getUsername(), empInfo.getName(), jwt);
        }else{
            return null;
        }
    }
}
